package controller

import (
	"github.com/gin-gonic/gin"
	"k8s-dash/dto"
	"k8s-dash/middleware"
	"k8s-dash/service"
	"k8s-dash/utils"
	"k8s-dash/utils/logger"
)

type stsController struct{}

var StsController stsController

func (p *stsController) Register(ginRouter *gin.RouterGroup) {
	nd := &stsController{}
	ginRouter.GET("/k8s/statefulSet/list", nd.GetStsList)
	ginRouter.GET("/k8s/statefulSet/detail", nd.GetStsDetail)
	ginRouter.DELETE("/k8s/statefulSet/del", nd.DeleteSts)
	ginRouter.PATCH("/k8s/statefulSet/update", nd.UpdateSts)
	ginRouter.GET("/k8s/statefulSet/numnp", nd.GetStsToNS)
	ginRouter.PUT("/k8s/statefulSet/restart", nd.RestartSts)
	ginRouter.GET("/k8s/statefulSet/scale", nd.ScaleSts)

}

// GetStsList 获取statefulSet列表，支持分页过滤排序
// ListPage godoc
// @Summary      获取statefulSet列表
// @Description  获取statefulSet列表
// @Tags         statefulSet
// @ID           /api/k8s/statefulSet/list
// @Accept       json
// @Produce      json
// @Param        filter_name  query  string  false  "过滤"
// @Param        namespace    query  string  false  "命名空间"
// @Param        page         query  int     false  "页码"
// @Param        limit        query  int     false  "分页限制"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": service.StsResp}"
// @Router       /api/k8s/statefulSet/list [get]
func (d *stsController) GetStsList(ctx *gin.Context) {
	parmas := &dto.StsListInput{}
	//参数验证
	err := parmas.BindValidate(ctx)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
		return
	}
	stsList, err := service.Statefulset.GetStsList(parmas.FilterName, parmas.Namespace, parmas.Limit, parmas.Page)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, stsList)
}

// GetStsDetail statefulSet
// ListPage godoc
// @Summary      statefulSet
// @Description  statefulSet
// @Tags         statefulSet
// @ID           /api/k8s/statefulSet/detail
// @Accept       json
// @Produce      json
// @Param        name  query  string  true  "name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": appsV1.statefulSet}"
// @Router       /api/k8s/statefulSet/detail [get]
func (d *stsController) GetStsDetail(ctx *gin.Context) {
	parmas := &dto.StsNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	stsDetail, err := service.Statefulset.GetStsDetail(parmas.Name, parmas.Namespace)
	if err != nil {
		return
	}
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, stsDetail)
}

// DeleteSts statefulSet
// ListPage godoc
// @Summary      statefulSet
// @Description  statefulSet
// @Tags         statefulSet
// @ID           /api/k8s/statefulSet/delete
// @Accept       json
// @Produce      json
// @Param        name  query  string  true  "name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/statefulSet/delete [delete]
func (d *stsController) DeleteSts(ctx *gin.Context) {
	parmas := &dto.StsNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Statefulset.DeleteSts(parmas.Name, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.DeleteError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.DeleteError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}

// UpdateSts statefulSet
// ListPage godoc
// @Summary      statefulSet
// @Description  statefulSet
// @Tags         statefulSet
// @ID           /api/k8s/statefulSet/update
// @Accept       json
// @Produce      json
// @Param        content  query  string  true  "更新内容"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/statefulSet/update [patch]
func (d *stsController) UpdateSts(ctx *gin.Context) {
	parmas := &dto.StsUpdateInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Statefulset.UpdateSts(parmas.Content, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.UpdateError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.UpdateError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}

// GetStsToNS statefulSet
// ListPage godoc
// @Summary      statefulSet
// @Description  statefulSet
// @Tags         statefulSet
// @ID           /api/k8s/statefulSet/deployNp
// @Accept       json
// @Produce      json
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": string}"
// @Router       /api/k8s/statefulSet/numnp [get]
func (p *stsController) GetStsToNS(ctx *gin.Context) {
	stsToNS, err := service.Statefulset.GetStsToNS()
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, stsToNS)
}

// ScaleSts statefulSet
// ListPage godoc
// @Summary      statefulSet
// @Description  statefulSet
// @Tags         statefulSet
// @ID           /api/k8s/statefulSet/scale
// @Accept       json
// @Produce      json
// @Param        name  query  string  true  "deploy_name"
// @Param        namespace    query  string  true  "命名空间"
// @Param        scale_num    query  int  true  "副本数"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/statefulSet/scale [get]
func (d *stsController) ScaleSts(ctx *gin.Context) {
	parmas := &dto.StsScaleInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	num, err := service.Statefulset.ScaleSts(parmas.Name, parmas.Namespace, parmas.ScaleNum)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, num)
}

// RestartSts statefulSet
// ListPage godoc
// @Summary      statefulSet
// @Description  statefulSet
// @Tags         statefulSet
// @ID           /api/k8s/statefulSet/restart
// @Accept       json
// @Produce      json
// @Param        name  query  string  true  "deploy_name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/statefulSet/restart [put]
func (d *stsController) RestartSts(ctx *gin.Context) {
	parmas := &dto.StsNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Statefulset.RestartSts(parmas.Name, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.UpdateError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.UpdateError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}
